package com.zymail.controller;

import com.zymail.bean.ZyGoodImg;
import com.zymail.entity.DataJson;
import com.zymail.service.ZyGoodImgService;
import com.zymail.util.UploadPhoto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;

@Controller
@RequestMapping ("/upload")
public class UploadController {

    @Autowired
    public ZyGoodImgService zyGoodImgService;

    @RequestMapping("/image")
    @ResponseBody
    public DataJson uploadImage(@RequestParam(value = "file") MultipartFile multipartFile) {
        String imagePath = UploadPhoto.upload(multipartFile);
        DataJson dataJson = new DataJson();
        if (imagePath != null) {
            dataJson.setCode(200);
            dataJson.setMsg("上传成功");
            HashMap<String, String> map = new HashMap<>();
            map.put("url", imagePath);
            dataJson.setData(map);
        } else {
            dataJson.setCode(0);
            dataJson.setMsg("对不起，上传失败");
        }
        return dataJson;
    }

    @ResponseBody
    @RequestMapping("addImage")
    public String addImage(String goodsImage, Integer type) {
        return "成功";
    }

}
